package com.gxs.ea.fileupload.beans.jsf;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.gxs.ea.fileportal.domain.BaseCompany;
import com.gxs.ea.fileupload.beans.data.model.User;
import com.gxs.ea.fileupload.beans.data.service.CompanyService;
import com.gxs.ea.fileupload.beans.utils.AuthUtils;
import com.gxs.ea.fileupload.beans.utils.JSFUtils;

@Component
@Scope("request")
public class ProfileBean  implements Serializable
{
	private String userName;
	private String userRoles;
	private BaseCompany selectedCompany;
	
	@Autowired
	private CompanyService companyService;
	
	@Autowired
	private JSFUtils jsfUtils;
	
	@Autowired
	private AuthUtils authUtils;
	
	private boolean init = false;
	
	public boolean isInit() {
		return init;
	}

	public void setInit(boolean init) {
		this.init = init;
		if(init)
		{
			 this.initialize();
		}
	}

	private void initialize() 
	{

		User user = authUtils.getDetailsForLoggedInUser();
		
		this.userName = user.getUsername();
		this.userRoles = String.valueOf(user.getAuthorities());
		this.selectedCompany = user.getCompany();
		
		if(user.getCompany() == null)
		{
			jsfUtils.printError("Invalid Company Assignment", "Your user id is not assigned to a valid company");
		}
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserRoles() {
		return userRoles;
	}

	public void setUserRoles(String userRoles) {
		this.userRoles = userRoles;
	}

	public BaseCompany getSelectedCompany() {
		return selectedCompany;
	}

	public void setSelectedCompany(BaseCompany selectedCompany) {
		this.selectedCompany = selectedCompany;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public JSFUtils getJsfUtils() {
		return jsfUtils;
	}

	public void setJsfUtils(JSFUtils jsfUtils) {
		this.jsfUtils = jsfUtils;
	}

	public AuthUtils getAuthUtils() {
		return authUtils;
	}

	public void setAuthUtils(AuthUtils authUtils) {
		this.authUtils = authUtils;
	}

}
